import 'package:flutter/material.dart';
import 'package:lib_uikit/lib_uikit.dart';

import '../../resource/resources.dart';
import '../../utils/gt_vip_utils.dart';

/// @Title  用户头像组件
/// @Author: bryce
/// @CreateDate: 2023年11月30日23:33:20
class GTAvatar extends StatelessWidget {
  final String? imageUrl;
  final String? imagePath;
  final String? userName; // 用户昵称，用于在没有头像时显示用户后两位字母
  final int vipLevel; // 会员等级，用于在使用用户名称时的背景色判断
  final double width;
  final double height;
  final bool? showPlaceholder;
  final bool? showErrorPlaceholder;
  // 圆角大小,如果不设置默认圆形头像，即radiusAll取宽高的一半
  double? radiusAll, radiusTopLeft, radiusTopRight, radiusBottomLeft, radiusBottomRight;

  final double fontSize; // 文字大小，用于在使用用户名称时

  /// Will resize the image in memory to have a certain width using [ResizeImage]
  final int? cacheWidth;
  /// Will resize the image in memory to have a certain height using [ResizeImage]
  final int? cacheHeight;

  GTAvatar(
      {Key? key,
      this.imageUrl = '',
      this.userName,
      this.vipLevel = 0,
      required this.width,
      required this.height,
      this.showPlaceholder,
      this.showErrorPlaceholder,
      this.radiusAll,
      this.radiusTopLeft,
      this.radiusTopRight,
      this.radiusBottomLeft,
      this.radiusBottomRight,
      this.fontSize = 19,
      this.imagePath,
      this.cacheWidth,
      this.cacheHeight})
      : assert(width > 0 && height > 0, '宽高不能小于0'),
        assert(imagePath != null || imageUrl != null || userName != null, '头像和昵称至少设置一个'),
        super(key: key);

  @override
  Widget build(BuildContext context) {
    if (null == radiusAll &&
        null == radiusTopLeft &&
        null == radiusTopRight &&
        null == radiusBottomLeft &&
        null == radiusBottomRight) {
      radiusAll = width / 2;
    }
    if (TextUtils.isNotEmpty(imageUrl)) {
      return GTImage(
        url: imageUrl,
        width: width,
        height: height,
        cacheWidth: cacheWidth,
        cacheHeight: cacheHeight,
        radiusAll: radiusAll ?? 0,
        showPlaceholder: showPlaceholder ?? true,
        showErrorPlaceholder: showErrorPlaceholder ?? true,
        radiusTopLeft: radiusTopLeft ?? 0,
        radiusTopRight: radiusTopRight ?? 0,
        radiusBottomLeft: radiusBottomLeft ?? 0,
        radiusBottomRight: radiusBottomRight ?? 0,
        placeholderPath: Images.defaultAvatar,
        errorPath: Images.defaultAvatar,
      );
    } else if (TextUtils.isNotEmpty(imagePath)) {
      return ClipOval(
        child: Image.asset(imagePath ?? Images.defaultAvatar, height: width, width: height,fit: BoxFit.cover,),
      );

    } else {
      final String lastTwoName = getNameLastTwo(userName!);
      return Container(
          width: width,
          height: height,
          alignment: Alignment.center,
          decoration: BoxDecoration(
              color: _avatarBgColor(),
              borderRadius: null != radiusAll
                  ? BorderRadius.circular(radiusAll!)
                  : BorderRadius.only(
                      topLeft: Radius.circular(radiusTopLeft ?? 0),
                      topRight: Radius.circular(radiusTopRight ?? 0),
                      bottomLeft: Radius.circular(radiusBottomLeft ?? 0),
                      bottomRight: Radius.circular(radiusBottomRight ?? 0))),
          child: Text(
            lastTwoName,
            style: TextStyle(fontSize: fontSize, color: GTVipUtils.getVipColorByVipLevel(vipLevel)),
          ));
    }
  }

  String getNameLastTwo(String userName) {
    if (userName.isEmpty) return '';
    if (userName.length < 2) return userName;

    String lastTwo = userName.substring(userName.length - 2);
    if ("**" == lastTwo && userName.length >= 4) {
      lastTwo = userName.substring(userName.length - 4, userName.length - 2);
    } else if ("**" == lastTwo && userName.length < 4) {
      lastTwo = userName.substring(0, 1);
    } else if (userName.indexOf("@") >= 2) {
      lastTwo = userName.substring(userName.indexOf("@") - 2, userName.indexOf("@"));
    }

    String temp;
    for (int i = lastTwo.length; i > 0; i--) {
      temp = lastTwo.substring(i - 1, i);
      if (_isChineseCharacter(temp)) {
        return temp;
      }
    }

    return lastTwo.toUpperCase().replaceAll("*", "");
  }

  bool _isChineseCharacter(String character) {
    final regex = RegExp(r'[\u4e00-\u9fa5]');
    return regex.hasMatch(character);
  }

  Color _avatarBgColor() {
    Color color = GTVipUtils.getVipColorByVipLevel(vipLevel);
    int red = color.red;
    int green = color.green;
    int blue = color.blue;
    return Color.fromARGB(26, red, green, blue);
  }
}
